package com.dcivision.framework.taglib.channel;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.Utility;

public class AjaxEventProcessor extends AbstractAjaxProcessor {
  
  private static final Log log = LogFactory.getLog(AjaxEventProcessor.class);
 
  /**
   * 
   * @param servletContext
   * @param servletConfig
   * @param request
   * @param response
   * @param session
   * @param parameters
   * @param actionType
   * @return
   * @throws ClassNotFoundException 
   * @throws IllegalAccessException 
   * @throws InstantiationException 
   * @throws NoSuchMethodException 
   * @throws SecurityException 
   * @throws InvocationTargetException 
   * @throws IllegalArgumentException 
   * @throws ApplicationException 
   */
   public String execute(ServletContext servletContext,ServletConfig servletConfig,HttpServletRequest request,HttpServletResponse response,HttpSession session,String[] parameters,String actionType,String methodName) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException, ApplicationException{
     try{
     
        this.initPageContext(servletContext, servletConfig, request, response, session);
        String className = (String)AjaxConstant.ACTIONMAP.get(actionType);
        String defaultMethodName="execute";
        Class[] parameterTypes = new Class[]{String[].class};
        Object[] args = new Object[]{parameters };
     
        if(Utility.isEmpty(methodName))
          methodName = defaultMethodName;
     
       if(!Utility.isEmpty(className)){
       //do bussiness job
       //invoke method
         Class c = Class.forName(className);
         Object obj=Class.forName(className).newInstance();
         Method before_Method = c.getMethod("init", new Class[]{AjaxPageContext.class});
         Method method = c.getDeclaredMethod(methodName, parameterTypes);
         Method after_Method = c.getMethod("release", null);
         Object initResult = before_Method.invoke(obj, new Object[]{threadContext.getPageContext()});//Before execute any method should init() first
         Object result=method.invoke(obj, args);
         Object destory=after_Method.invoke(obj, null);//release it
       }
     
       threadContext.getConnnection().commit();
     }catch(Exception ex){
       log.error(ex, ex);
     }finally{
       try {
         threadContext.closeConnection();//release connection
       } catch (Exception e) {
         log.error(e, e);
       }
     }
     return null;
   }
}
